package com.dempe.forest.common;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Created with IntelliJ IDEA.
 * User: Dempe
 * Date: 2016/4/8
 * Time: 15:36
 * To change this template use File | Settings | File Templates.
 */
public class ZooKeeperFactory {

    public String connStr = "172.16.3.42:2181,172.16.3.65:2181,172.16.3.24:2181";

    public int maxRetries = 3;

    public int sleepTime = 3000;

    public String nameSpace = "cfg";


    public ZooKeeperFactory(String connStr, String nameSpace, int sleepTime, int maxRetries) {
        this.connStr = connStr;
        this.nameSpace = nameSpace;
        this.sleepTime = sleepTime;
        this.maxRetries = maxRetries;
    }

    public CuratorFramework get() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(sleepTime, maxRetries);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(connStr)
                .retryPolicy(retryPolicy)
                .namespace(nameSpace)
                .build();
        client.start();
        return client;
    }
}
